package xymt.novaway.system.service.impl;

import xymt.novaway.common.core.utils.MapstructUtils;
import xymt.novaway.common.mybatis.core.page.TableDataInfo;
    import xymt.novaway.common.core.utils.StringUtils;
import xymt.novaway.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import xymt.novaway.system.domain.bo.SysTodoBo;
import xymt.novaway.system.domain.vo.SysTodoVo;
import xymt.novaway.system.domain.SysTodo;
import xymt.novaway.system.mapper.SysTodoMapper;
import xymt.novaway.system.service.ISysTodoService;
import xymt.novaway.system.annotation.DataScope;
import xymt.novaway.system.utils.DataScopeUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 待办事项Service业务层处理
 *
 * @author NovaWay
 * @date 2025-08-04
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysTodoServiceImpl implements ISysTodoService {

    private final SysTodoMapper baseMapper;

    /**
     * 查询待办事项
     *
     * @param todoId 主键
     * @return 待办事项
     */
    @Override
    public SysTodoVo queryById(Long todoId){
        return baseMapper.selectVoById(todoId);
    }

    /**
     * 分页查询待办事项列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 待办事项分页列表
     */
    @DataScope(permission = "system:todo:list", enableAdvancedFieldPermission = true)
    @Override
    public TableDataInfo<SysTodoVo> queryPageList(SysTodoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysTodo> lqw = buildQueryWrapper(bo);
        Page<SysTodoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的待办事项列表
     *
     * @param bo 查询条件
     * @return 待办事项列表
     */
    @DataScope(permission = "system:todo:list", enableAdvancedFieldPermission = true)
    @Override
    public List<SysTodoVo> queryList(SysTodoBo bo) {
        LambdaQueryWrapper<SysTodo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysTodo> buildQueryWrapper(SysTodoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysTodo> lqw = Wrappers.lambdaQuery();
        // 按照更新时间降序排列，最新的待办事项排在前面
        lqw.orderByDesc(SysTodo::getUpdateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), SysTodo::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SysTodo::getContent, bo.getContent());
        lqw.eq(bo.getCompleted() != null, SysTodo::getCompleted, bo.getCompleted());
        
        // 应用数据权限过滤
        DataScopeUtils.applyDataScopeFilter(lqw);
        
        return lqw;
    }

    /**
     * 新增待办事项
     *
     * @param bo 待办事项
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysTodoBo bo) {
        SysTodo add = MapstructUtils.convert(bo, SysTodo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTodoId(add.getTodoId());
        }
        return flag;
    }

    /**
     * 修改待办事项
     *
     * @param bo 待办事项
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysTodoBo bo) {
        SysTodo update = MapstructUtils.convert(bo, SysTodo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysTodo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除待办事项信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
